package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Answer;
import cn.wolfcode.domain.Exam;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;


    @RequiredPermission(name="考试页面",expermission="exam:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo,Long id){
        List<Answer> list = examService.selectByItemId(id);
        model.addAttribute("exam",examService.get(id));
        model.addAttribute("list",list);
        return "exam/list";
    }


    @RequiredPermission(name="考试删除",expermission="exam:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           examService.delete(id);
        }
        return "redirect:/exam/list";
    }


    @RequiredPermission(name="考试新增/编辑",expermission="exam:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Exam exam) {
        if (exam.getId() == null) {
            examService.save(exam);
        } else {
            examService.update(exam);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setId(exam.getId());
        return jsonResult;
    }


    @RequiredPermission(name="考试新增/编辑",expermission="exam:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("exam",examService.get(id));
        }
        return "/exam/input";
    }
}
